package com.yunang.officialweb.controller;

import com.yunang.officialweb.common.R;
import com.yunang.officialweb.entity.Department;
import com.yunang.officialweb.service.DepService;
import com.yunang.officialweb.service.NewsService;
import com.yunang.officialweb.service.TotalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author duan
 * @date 2022/11/29 21:17
 */
@RestController
@RequestMapping("/OfficialWebsite")
public class DepController {
    @Autowired
    private DepService depService;
    @Autowired
    private TotalService totalService;

    @RequestMapping(value = "/getDep", method = RequestMethod.GET)
    public R getDep(@RequestParam int id){
        return R.ok(depService.selectById(id));
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public R findAll(){
        return R.ok(depService.findAll());
    }

    @RequestMapping(value = "/updateDep", method = RequestMethod.PUT)
    public R updateDep(@RequestParam int id,
                       @RequestParam(required = false) MultipartFile file,
                       @RequestParam(required = false) String name,
                       @RequestParam(required = false) String desc1,
                       @RequestParam(required = false) String desc2){
        Department department = new Department();
        department.setId(id);
        department.setImage(totalService.savePic(file, "D://YunAng-web//joinUs//"));
        department.setName(name);
        department.setDesc1(desc1);
        department.setDesc2(desc2);
        return R.ok(depService.updateDep(department));
    }
}
